package com.lord.otter.component.exception;

/**
 * 异常码接口
 *
 * @author lord
 * 2025/7/2 11:18
 */
public interface BusinessExceptionEnumAble {

    /**
     * 业务代码
     *
     * @author lord
     * 2025/7/2 11:20
     */
    String getBusinessCode();

    /**
     * 异常代码
     *
     * @author lord
     * 2025/7/2 12:31
     */
    String getExceptionCode();

    /**
     * 异常代码 业务码+异常码
     *
     * @author lord
     * 2025/7/2 11:19
     */
    default String getCode() {
        return getBusinessCode() + getExceptionCode();
    }

    /**
     * 异常消息
     *
     * @author lord
     * 2025/7/2 11:20
     */
    String getMessage();

    default BusinessException exceptionError(String message) {
        return new BusinessException(getCode(), message);
    }

    default BusinessException exceptionDefault() {
        return exception(getMessage(), getCode(), (String) null);
    }

    default BusinessException exceptionMessage(String message) {
        return exception(message, getCode(), (String) null);
    }

    default BusinessException exceptionArgs(String... args) {
        return exception(getMessage(), getCode(), args);
    }

    default BusinessException exceptionMessageArgs(String message, String... args) {
        return exception(message, getCode(), args);
    }

    default BusinessException exception(String message, String code, String... args) {
        if (args == null || args.length == 0) {
            message = message.replaceAll("\\{[^}]*}", "");
        } else {
            for (int i = 0; i < args.length; i++) {
                String arg = args[i];
                message = message.replaceAll("\\{" + i + "}", arg);
            }
        }
        return new BusinessException(code, message);
    }
}
